
// console.log("index,run");
// 发起请求函数
function ajax({
    method,
    url,
    success
}) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url, true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (this.status == 200 && this.readyState == 4) {
            var res = JSON.parse(this.responseText);
            success(res);
        }
    }
}
// 数据渲染函数
function handData(num) {
    for (let i = 0; i < num.length; i++) {
        var html = `<img src=${num[i].imageUrl} alt="">`;
        // console.log("dd");
        $(".list-roll").append(html);
    }
}
// 数据渲染函数
function twoHandData(num) {
    for (let i = 0; i < 8; i++) {
        var x = listen(num, i);
        var song = `<div class="m-l-one-song">
                    <a href="html/detail.html?value=${num[i].id}">
                        <img style="width: 140px;height: 140px;" src=${num[i].coverImgUrl} alt="">
                    </a>
                    
                    <div class="song-play">
                        <p></p>
                        <span>${x}</span>
                        <p></p>
                    </div>
                    <p>
                        <a href="javascript:void(0)">${num[i].name}</a>
                    </p>
                </div>`;
        // console.log("dd");
        $(".l-o-bottom").append(song);
    }
}
//听的人数处理
function listen(num, i) {
    if (num[i].playCount > 10000) {
        var x = Math.round(num[i].playCount / 10000) + "万";
    }
    else if (num[i].playCount > 100000000) {
        var x = Math.round(num[i].playCount / 10000) + "亿";
    }
    else { var x = Math.round(num[i].playCount) }
    return x;
}
// 请求调用
ajax({
    method: "get",
    url: "http://192.168.4.18:3000/banner",
    success: function (item) {
        // console.log(typeof (item));
        // console.log(item.banners);
        handData(item.banners);
        // console.log(item.banners.length);
    }
})
// 请求调用
ajax({
    method: "get",
    url: "http://192.168.4.18:3000/top/playlist?cat=%E5%8D%8E%E8%AF%AD",
    success: function (item) {
        // console.log(typeof (item.playlists));
        // console.log(item.playlists);
        // console.log(typeof( item.playlists[1].playCount));
        twoHandData(item.playlists);
    }


})
